import request from '@/utils/request'
import { IFormRule } from './types/form'
import {
  Product,
  ProductType,
  ProductListParams,
  ProductCategory,
  AttrRuleValue,
  ProductAttrTpl,
  IExpressTemplate,
  AttrTableHeader,
  ProductAttr,
  CategoryListParams,
  CategoryParentOption,
  CategoryForm,
  RuleForm,
  RuleListParams,
  Rule,
  RuleDetail,
  ReplyListParams,
  Reply,
  FictitiousReply
} from './types/product'

/**
 * 获取商品类目列表
 * @returns 商品类目列表
 */
export const getProductTypes = () => {
  return request<{
    list: ProductType[]
  }>({
    method: 'GET',
    url: '/product/product/type_header'
  })
}

/**
 * 获取商品列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getProducts = (params?: ProductListParams) => {
  return request<{
    count: number
    list: Product[]
  }>({
    method: 'GET',
    url: '/product/product',
    params
  })
}

/**
 * 保存新增或编辑
 * @param id 商品ID
 * @param data 商品数据
 * @returns null
 */
export const saveProduct = (id: number, data: any) => {
  return request({
    method: 'POST',
    url: `/product/product/${id}`,
    data
  })
}

/**
 * 获取商品
 * @param id 商品id
 * @returns 商品
 */
export const getProduct = (id: number) => {
  return request<{
    tempList: {
      id: number
      name: string
    }[]
    cateList: {
      value: number
      label: string
      disabled: number
    }[]
    productInfo: {
      cate_id: string[]
      is_sub: number[]
      activity: string[]
      label_id: string[]
      coupons: string[]
      description: string
      items: string[]
      attrs: any[]
      attr: {
        pic: string
        vip_price: number
        price: number
        cost: number
        ot_price: number
        stock: number
        bar_code: string
        weight: number
        volume: number
        brokerage: number
        brokerage_two: number
      }
    } & Omit<
      Product,
      | 'cate_id'
      | 'is_sub'
      | 'activity'
      | 'label_id'
      | 'collect'
      | 'likes'
      | 'visitor'
      | 'cate_name'
      | 'stock_attr'
    >
  }>({
    method: 'GET',
    url: `/product/product/${id}`
  })
}

export const getCategoryTree = (type: 0 | 1) => {
  return request<ProductCategory[]>({
    method: 'GET',
    url: `/product/category/tree/${type}`
  })
}

/**
 * 获取分类列表
 * @param params 查询参数
 * @returns 分类列表
 */
export const getProductCategorys = (params?: CategoryListParams) => {
  return request<{
    count: number
    list: ProductCategory[]
  }>({
    method: 'GET',
    url: '/product/category',
    params
  })
}

/**
 * 分类状态显示/隐藏操作
 */
export const updateCategoryStatus = (id: number, is_show: number) => {
  return request({
    method: 'PUT',
    url: `/product/category/set_show/${id}/${is_show}`
  })
}

/**
 * 分类删除
 */
export const removeCategory = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/category/${id}`
  })
}

/**
 * 获取分类父级选项列表
 * @returns 分类父级选项列表
 */
export const getCategoryParentOptions = () => {
  return request<{
    rules: [{ options: CategoryParentOption[]; field: string }]
  }>({
    method: 'GET',
    url: '/product/category/create'
  }).then(data => {
    const field = data.rules.find(rule => rule.field === 'pid')
    return field ? field.options : []
  })
}

/**
 * 保存分类
 * @param id
 * @param data
 * @returns
 */
export const saveCategory = (data: CategoryForm, id?: number | null) => {
  return request({
    method: id ? 'PUT' : 'POST',
    url: `/product/category${id ? '/' + id : ''}`,
    data
  })
}

/**
 * 获取分类信息
 * @param id
 * @returns
 */
export const getCategory = (id: number) => {
  return request<{
    rules: IFormRule[]
  }>({
    method: 'GET',
    url: `/product/category/${id}`
  }).then(data => {
    return data.rules
  })
}

/**
 * 商品上下架操作
 */
export const updateProductStatus = (id: number, status: number) => {
  return request({
    method: 'PUT',
    url: `/product/product/set_show/${id}/${status}`
  })
}

/**
 * 商品加入/移除回收站
 */
export const removeProduct = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/product/${id}`
  })
}

/**
 * 获取商品规格模板
 */
export const getAttrs = () => {
  return request<ProductAttrTpl[]>({
    method: 'GET',
    url: '/product/product/get_rule'
  })
}

/**
 * 生成商品属性
 */
export const generateAttr = (
  id: number,
  type: 0 | 1,
  data: {
    attrs: AttrRuleValue[]
  }
) => {
  return request<{
    info: {
      attr: AttrRuleValue[]
      header: AttrTableHeader[]
      value: ProductAttr[]
    }
  }>({
    method: 'POST',
    url: `/product/generate_attr/${id}/${type}`,
    data
  })
}

/**
 * 获取运费模板
 */
export const getExpressTemplate = () => {
  return request<IExpressTemplate[]>({
    method: 'GET',
    url: '/product/product/get_template'
  })
}

/**
 * 获取商品规格列表
 */
export const getProductRules = () => {
  return request({
    method: 'GET',
    url: '/product/product/rule'
  })
}

/**
 * 批量上架
 */
export const updateProductsShow = (ids: number[]) => {
  return request({
    method: 'PUT',
    url: '/product/product/product_show',
    data: {
      ids
    }
  })
}

/**
 * 批量下架
 */
export const updateProductsUnshow = (ids: number[]) => {
  return request({
    method: 'PUT',
    url: '/product/product/product_unshow',
    data: {
      ids
    }
  })
}

/**
 * 获取规格列表
 * @param params 查询参数
 * @returns 规格列表
 */
export const getRules = (params?: RuleListParams) => {
  return request<{
    count: number
    list: Rule[]
  }>({
    method: 'GET',
    url: '/product/product/rule',
    params
  })
}

/**
 * 获取规格详情
 * @param params 查询参数
 * @returns 规格列表
 */
export const getRuleDetail = (id: number) => {
  return request<RuleDetail>({
    method: 'GET',
    url: `/product/product/rule/${id}`
  })
}

/**
 * 保存或更新规格
 * @param data
 * @param id
 * @returns
 */
export const saveAttrRule = (data: RuleForm, id: number = 0) => {
  return request({
    method: 'POST',
    url: `/product/product/rule/${id}`,
    data
  })
}

/**
 * 删除规格
 */
export const removeRule = (params: {
  ids: string
  all?: string
  where?: string
}) => {
  return request({
    method: 'DELETE',
    url: '/product/product/rule/delete',
    data: params
  })
}

/**
 * 获取评论列表
 * @param params
 * @returns
 */
export const getReplys = (params: ReplyListParams) => {
  return request<{
    count: number
    list: Reply[]
  }>({
    method: 'GET',
    url: '/product/reply',
    params
  })
}

/**
 * 删除评论
 * @param id
 * @returns
 */
export const removeReply = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/reply/${id}`
  })
}

/**
 * 回复评论或修改回复
 * @param id
 * @param content
 * @returns
 */
export const replyCommentById = (id: number, content: string) => {
  return request({
    method: 'PUT',
    url: `/product/reply/set_reply/${id}`,
    data: {
      content
    }
  })
}

/**
 * 获取虚拟评论表单验证规则
 * @param product_id
 * @returns
 */
export const getFictitiousReplyFormOptions = (product_id: number = 0) => {
  return request<{
    rules: IFormRule[]
  }>({
    method: 'GET',
    url: `/product/reply/fictitious_reply/${product_id}`
  }).then(data => {
    return data.rules
  })
}

/**
 * 添加虚拟评论
 * @param data
 * @returns
 */
export const saveFictitiousReply = (data: FictitiousReply) => {
  return request({
    method: 'POST',
    url: '/product/reply/save_fictitious_reply',
    data
  })
}
